/**
 * @class common.mask
 * @extends common.base.object
 * 微信风格遮罩
 */
define(function (require) {
    var Base = require('common/base/object'),
        Tpl = require('text!./templates/mask.html');
    var DEFAULT_TIMEOUT = 1.5;

    return Base.extend({
        /**
         *  用法1:
         *  mask.show("XXXXX");
         *  object形式 :
         *  {
         *      icon:"",
         *      content:'',
         *      timeout:'',
         *      fn:'',
         *  }
         * */
        show: function (params) {
            var _this = this;
            var mask_sel = '.common-wap-mask';
            $(mask_sel).remove();
            if (_.isString(params)) {
                params = {content: params};
            }
            params = params || {};
            window.Z_INDEX++;
            _.defaults(params, {
                z_index: window.Z_INDEX,
                icon: 'loading',
                content: '正在加载',
                fn: function () {

                }
            });
            _this.params = params;
            $('body').append(_this.render(Tpl, _this.params));
            $(mask_sel).css({
                'left': (document.documentElement.clientWidth - $(mask_sel).width()) / 2 + 'px',
            });
            $(window).resize(function () {
                $(mask_sel).css({
                    'left': (document.documentElement.clientWidth - $(mask_sel).width()) / 2 + 'px'
                });
            });
            if (params.timeout) {
                setTimeout(function () {
                    _this.hide();
                    if (params.fn) {
                        params.fn();
                    }
                }, params.timeout * 1000);
            }
        },
        error: function (content, fn) {
            this.show({
                icon: 'error',
                content: content,
                timeout: DEFAULT_TIMEOUT,
                fn: fn
            });
        },
        success: function (content, fn) {
            this.show({
                icon: 'success',
                content: content,
                timeout: DEFAULT_TIMEOUT,
                fn: fn
            });
        },
        hide: function () {
            $('.common-wap-mask').addClass('mask-hide').fadeOut();
        }
    });
});